<template>
  <div class="login-container">
    <img src="../../assets/bg.png" class="bg-img" />
    <div class="center">
      <div class="left">
        <img src="../../assets/logo-white.png" class="left-bg-img" />
        <a-divider />
        <div class="left-logo-title">纪检监察大数据监督平台</div>
      </div>
      <div class="form-wrapper">
        <div class="title">
          <div class="tab-title">账号登录</div>
        </div>
        <div class="item-wrapper mt-6">
          <a-input v-model="username" placeholder="请输入用户名/手机号" allow-clear size="large">
            <template #prefix>
              <icon-mobile />
            </template>
          </a-input>
        </div>
        <div class="item-wrapper mt-4">
          <a-input-password v-model="password" placeholder="请输入密码" allow-clear size="large">
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </div>
        <div class="item-wrapper mt-4">
          <a-input v-model="verifyCode" placeholder="请输入验证码" allow-clear size="large">
            <template #prefix>
              <icon-image />
            </template>
            <template #suffix>
              <img class="veify-code" :src="imageCode" alt="" @click="initImageCode" />
            </template>
          </a-input>
        </div>
        <!-- <div class="flex-1"></div> -->
        <div class="my-width mt-6">
          <div class="flex justify-between">
            <a-checkbox v-model="autoLogin">记住密码</a-checkbox>
            <!-- <a-link :underline="false" type="primary">忘记密码？</a-link> -->
          </div>
        </div>
        <div class="mt-6">
          <a-button type="primary" class="login" :loading="loading" @click="onLogin">
            登录
          </a-button>
        </div>
      </div>
    </div>
    <div class="bottom">技术支持：纪检监察大数据监督平台水网智慧科技有限公司</div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import ImageBg1 from '@/assets/img_login_bg_01.png'
import logo from '@/assets/logo.png'
import { post, Response } from '@/api/http'
import { Message } from '@arco-design/web-vue'
import setting from '../../setting'
import useAppInfo from '@/hooks/useAppInfo'
import useUserStore from '@/store/modules/user'
import UserService from '@/service/system/user'

export default defineComponent({
  name: 'Login',
  setup() {
    const projectName = setting.projectName
    const { version } = useAppInfo()
    const username = ref('')
    const password = ref('')
    const verifyCode = ref('')
    const autoLogin = ref(true)
    const loading = ref(false)
    const imageCode = ref('')
    const deviceId = ref('')
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    const redirectPath = route.query.redirect ? (route.query.redirect as string) : '/'
    // 判断是否已经登录
    const checkIsLogin = () => {
      // 如果token存在，则跳转到首页
      if (!userStore.isTokenExpire()) {
        router
          .replace({
            path: redirectPath,
          })
      }
    }
    checkIsLogin()
    const onLogin = () => {
      loading.value = true
      userStore.doLogin({
        username: username.value,
        password: password.value,
        deviceId: deviceId.value,
        imageCode: verifyCode.value,
      }).then((res: Response<any>) => {
        // type == 0 表示登录成功，不等于0表示要修改密码
        if (res.code == 200) { // 表示登录成功
          if (res.data.type == 0) { // 表示登录成功
            // 跳转到首页
            router
              .replace({
                path: redirectPath,
              })
              .then(() => {
                Message.success('登录成功，欢迎：' + username.value)
                loading.value = false
              })
          } else { // 表示需要修改密码

          }
        } else {
          loading.value = false
          // 重新获取验证码
          initImageCode()
        }
      }).catch(error => {
        // 登录失败
        loading.value = false
        Message.error(error.message)
        initImageCode()
      })
    }
    // 初始化验证码
    const initImageCode = () => {
      // 获取登录图片验证码
      UserService.createCode().then(res => {
        deviceId.value = res.data.deviceId;
        imageCode.value = res.data.imageContent;
      })
    }

    const handleEnterDown = (event: KeyboardEvent) => {
      if (event.key === 'Enter') {
        onLogin()
      }
    }
    // 渲染时获取系统配置信息
    onMounted(() => {
      initImageCode()
      window.addEventListener('keydown', handleEnterDown)
    })
    onUnmounted(() => {
      window.removeEventListener('keydown', handleEnterDown)
    })
    return {
      projectName,
      version,
      username,
      password,
      autoLogin,
      loading,
      onLogin,
      ImageBg1,
      logo,
      deviceId,
      imageCode,
      verifyCode,
      initImageCode
    }
  },
})
</script>

<style lang="less" scoped>
@leftWith: 35%;

.login-container {
  position: relative;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;

  .bg-img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }

  .bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 3%;
    font-size: 18px;
    color: #fff;
    text-align: center;
  }

  .center {
    position: relative;
    z-index: 9;
    width: 70%;
    height: 60%;
    display: flex;
    align-items: center;

    .left {
      position: relative;
      width: 50%;
      height: 100%;
      padding: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      text-align: center;
      margin-right: 60px;

      .left-bg-img {
        width: 90%;
      }

      :deep(.arco-divider) {
        border-bottom: 1px solid #fff;
      }

      .left-logo-title {
        color: #fff;
        font-size: 50px;
      }

      .proj-name {
        font-size: 30px;
        font-weight: bold;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    .form-wrapper {
      background-color: #fff;
      border-radius: 10px;
      border: 1px solid #f5f5f5;
      width: 40%;
      padding: 4% 3%;
      height: 100%;
      display: flex;
      flex-direction: column;

      .arco-input-wrapper {
        height: 60px;
        border-radius: 10px;
      }

      .title {
        font-size: 24px;
        font-weight: bold;
        box-shadow: 0 1px 0 0 #ccc;

        /* 添加这一行 */
        .tab-title {
          padding-bottom: 20px;
          width: 100px;
          box-shadow: 0 2px 0 0 #000;
          /* 添加这一行 */
        }
      }

      .login {
        width: 100%;
        height: 60px;
        font-size: 20px;
        border-radius: 10px;
      }
    }
  }
}

@media screen and(max-width: 966px) {
  .left {
    display: none;
  }

  .right {
    background-image: url('../../assets/img_login_mobile_bg_01.jpg');
    background-size: cover;

    .form-wrapper {
      width: 80% !important;
    }
  }
}
</style>
